﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using ExtAspNet;
using MyDMS.DAL;
using ListItem = ExtAspNet.ListItem;

namespace MyDMS_ExtJS
{
    public partial class UploadDocument : PageBase
    {
        private MyDMSEntity myDmsEntity = new MyDMSEntity();
        private int _task_code = 0;
        private int _proj_code = 0;
        private int _dept_code = 0;
        private int _folder_code = 0;
        protected void Page_Load(object sender, EventArgs e)
        {

            if (Request.QueryString["task"] != null)
            {
                _task_code = int.Parse(Request.QueryString["task"]);
            }
            else
            {
                ddlTask.Enabled = true;
            }
            if (Request.QueryString["proj"] != null)
            {
                _proj_code = int.Parse(Request.QueryString["proj"]);
            }
            if (Request.QueryString["dept"] != null)
            {
                _dept_code = int.Parse(Request.QueryString["dept"]);
            }
            if (Request.QueryString["folder"] != null)
            {
                _folder_code = int.Parse(Request.QueryString["folder"]);
            }
            else
            {
                ddlFolder.Enabled = true;
            }

            #region 文档类型

            List<DocumentType> docTypes = myDmsEntity.DocumentType.ToList();

            ddlDocumentTypes.DataSource = docTypes;
            ddlDocumentTypes.DataTextField = "Type_Name";
            ddlDocumentTypes.DataValueField = "DT_Code";
            ddlDocumentTypes.DataBind();
            ddlDocumentTypes.Items.Insert(0, new ListItem("选择文档类型", "-1"));

            #endregion

            #region 密级

            List<SecretLevel> secretLevels = myDmsEntity.SecretLevel.ToList();

            ddlSecretLevel.DataSource = secretLevels;
            ddlSecretLevel.DataTextField = "SecretLevel_Name";
            ddlSecretLevel.DataValueField = "SecretLevel_Code";
            ddlSecretLevel.DataBind();
            ddlSecretLevel.Items.Insert(0, new ListItem("选择密级", "-1"));

            #endregion

            #region 保存期限

            List<ShelfLife> shelfLifes = myDmsEntity.ShelfLife.ToList();

            ddlShelfLife.DataSource = shelfLifes;
            ddlShelfLife.DataTextField = "ShelfLife_Name";
            ddlShelfLife.DataValueField = "ShelfLife_Code";
            ddlShelfLife.DataBind();
            ddlShelfLife.Items.Insert(0, new ListItem("选择保存期限", "-1"));

            #endregion

            #region Tasks

            List<MyDMS.DAL.Task> tasks = myDmsEntity.Task.Where(t => t.Project_Code == _proj_code).ToList();

            ddlTask.DataSource = tasks;
            ddlTask.DataTextField = "Task_Title";
            ddlTask.DataValueField = "Task_Code";
            ddlTask.DataBind();
            ddlTask.Items.Insert(0, new ListItem("选择小项", "-1"));

            ddlTask.SelectedValue = _task_code.ToString();

            #endregion

            #region Folders

            List<MyDMS.DAL.Folder> folders = myDmsEntity.Folder.Where(t => t.Task_Code == _task_code).ToList();

            ddlFolder.DataSource = folders;
            ddlFolder.DataTextField = "Folder_Title";
            ddlFolder.DataValueField = "Folder_Code";
            ddlFolder.DataBind();
            ddlFolder.Items.Insert(0, new ListItem("选择分项", "-1"));

            ddlFolder.SelectedValue = _folder_code.ToString();

            #endregion

            dpUploadDate.SelectedDate = DateTime.Now;
            tbxOwner.Text = Author.user_Name;
        }

        protected void btnSubmit_Click(object sender, EventArgs e)
        {
            Document uDoc = new Document();
            string filePath = string.Empty;
            uDoc.Document_Name = txtDocName.Text;
            uDoc.Document_Desc = txtDocDesc.Text;

            if (_task_code != 0)
            {
                uDoc.Task_Code = _task_code;
                filePath =
                    Server.MapPath(string.Format("Upload/UserData/Files/{0}/{1}/{2}/", _dept_code, _proj_code,
                                                 _task_code));
            }
            if (_folder_code != 0)
            {
                uDoc.Folder_Code = _folder_code;
                filePath =
                    Server.MapPath(string.Format("Upload/UserData/Files/{0}/{1}/{2}/{3}/", _dept_code, _proj_code, _task_code,
                                                 _folder_code));
            }
            else
            {
                if (string.IsNullOrEmpty(ddlFolder.SelectedValue))
                {
                    uDoc.Folder_Code = null;
                }
                else
                {
                    _folder_code = Convert.ToInt32(ddlFolder.SelectedValue);
                    uDoc.Folder_Code = _folder_code;
                    filePath =
                        Server.MapPath(string.Format("Upload/UserData/Files/{0}/{1}/{2}/", _dept_code, _proj_code, _task_code));
                }
            }

            if (!System.IO.Directory.Exists(filePath))
            {
                System.IO.Directory.CreateDirectory(filePath);
            }
            filePath += fluDocument.FileName;
            fluDocument.SaveAs(filePath);

            uDoc.File_Path = filePath;
            uDoc.Recieved_Date = dpUploadDate.SelectedDate.Value;
            uDoc.Creation_Date = DateTime.Now;
            uDoc.Updation_Date = DateTime.Now;
            uDoc.ShelfLife_Code = int.Parse(ddlShelfLife.SelectedValue);
            uDoc.SecretLevel_Code = int.Parse(ddlSecretLevel.SelectedValue);

            uDoc.Document_Type = int.Parse(ddlDocumentTypes.SelectedValue);
            uDoc.Owner = Author.user_Code;

            myDmsEntity.Document.AddObject(uDoc);

            if (myDmsEntity.SaveChanges() > 0)
            {
                //更新分项
                if (_folder_code != 0)
                {
                    MyDMS.DAL.Folder folder = myDmsEntity.Folder.FirstOrDefault(f => f.Folder_Code == _folder_code);
                    if (folder != null) folder.Updation_Date = DateTime.Now;
                    if (myDmsEntity.SaveChanges() <= 0)
                    {
                        Alert.ShowInTop("文件已保存，更新分项时出错");
                    }
                }

                //更新小项 
                MyDMS.DAL.Task task = myDmsEntity.Task.FirstOrDefault(r => r.Task_Code == _task_code);
                task.Updation_Date = DateTime.Now;
                if (myDmsEntity.SaveChanges() <= 0)
                {
                    Alert.ShowInTop("文件已保存，更新小项时出错");
                }

                //更新中项
                MyDMS.DAL.Project proj = myDmsEntity.Project.FirstOrDefault(p => p.Project_Code == _proj_code);
                proj.Updation_Date = DateTime.Now;
                if (myDmsEntity.SaveChanges() < 0)
                {
                    Alert.ShowInTop("文件已保存，更新中项时出错");
                }
                Alert.ShowInTop("文件已成功保存");
            }
            else
            {

                Alert.ShowInTop("文件保存失败");
            }

            PageContext.RegisterStartupScript(ActiveWindow.GetHidePostBackReference());
        }
    }
}